package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.api.common.ResUlt;
import top.api.domain.dto.PageDto;
import top.api.domain.dto.StudentAddDto;
import top.api.domain.dto.StudentEditDto;
import top.api.domain.entity.Class;
import top.api.domain.entity.Student;
import top.api.domain.vo.PageVo;
import top.api.mapper.ClassMapper;
import top.api.service.StudentService;
import top.api.mapper.StudentMapper;
import org.springframework.stereotype.Service;
import top.api.utils.AssertUtil;
import top.api.utils.BeanCopyUtils;

import java.util.List;

/**
* @author 27465
* @description 针对表【student】的数据库操作Service实现
* @createDate 2023-12-16 23:17:47
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{
    private final ClassMapper classMapper;

    @Override
    public ResUlt<?> pageList(PageDto pageDto, Integer id, Integer classno, String name) {
        Page<Student> pageInfo = new Page<>(pageDto.getPageNum(),pageDto.getPageSize());

        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotNull(id),Student::getId,id);
        queryWrapper.eq(ObjectUtils.isNotNull(classno),Student::getClassno,classno);
        queryWrapper.like(StringUtils.isNotBlank(name),Student::getName,name);

        baseMapper.selectPage(pageInfo,queryWrapper);

        PageVo<Student> pageVo = new PageVo<>(pageInfo.getRecords(),pageInfo.getTotal());

        return ResUlt.success(pageVo);
    }

    @Override
    public ResUlt<?> getStudentInfo(Integer id) {
        return ResUlt.success(baseMapper.selectById(id));
    }

    @Override
    public ResUlt<?> addStudent(StudentAddDto studentAddDto) {
        Student student = BeanCopyUtils.copyBean(studentAddDto, Student.class);

        // 查询班级是否存在
        Class aClass = classMapper.selectById(studentAddDto.getClassno());
        AssertUtil.isNull(aClass,"班级ID错误");

        // 手动绑定班级ID
        student.setClassno(studentAddDto.getClassno().toString());

        int flag = baseMapper.insert(student);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> editStudent(StudentEditDto studentEditDto) {
        Student student = BeanCopyUtils.copyBean(studentEditDto, Student.class);

        if (ObjectUtils.isNotNull(studentEditDto.getClassno())){
            // 查询班级是否存在
            Class aClass = classMapper.selectById(studentEditDto.getClassno());
            AssertUtil.isNull(aClass,"班级ID错误");

            // 手动绑定班级ID
            student.setClassno(studentEditDto.getClassno().toString());
        }

        int flag = baseMapper.updateById(student);

        return ResUlt.successOrError(flag);
    }

    @Override
    public ResUlt<?> delStudent(List<Integer> ids) {
        int count = baseMapper.deleteBatchIds(ids);

        return ResUlt.success("删除成功: ["+count+"]");
    }
}




